﻿using ProjTemp.Desktop.Common.Container;
using ProjTemp.Desktop.Common.Controls;
using System.Windows;
using ProjTemp.Desktop.Common.ViewModel;
using System.Windows.Data;
using ProjTemp.Common.Utils;

namespace ProjTemp.Desktop.Common.View
{
    public class VBase : CustomMetroUserControl
    {
        public IContainer Container { get; set; }

        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(VBase), new PropertyMetadata(string.Empty));

        public object Header 
        {
            get { return GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        public VBase()
        {
            this.DataContextChanged += new DependencyPropertyChangedEventHandler(VBase_DependencyPropertyChanged);
        }

        protected virtual void VBase_DependencyPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (e.Property == FrameworkElement.DataContextProperty)
            {
                var newDataContext = e.NewValue as IPresenter;
                if (newDataContext != null)
                {
                    newDataContext.View = this;
                    Binding headerBinding = new Binding(DesktopConst.ViewHeaderPropertyName);
                    headerBinding.Source = newDataContext;
                    headerBinding.Mode = BindingMode.TwoWay;
                    this.SetBinding(HeaderProperty, headerBinding);
                }
            }
        }
    }
}
